<?php
    class Subject_book extends MY_controller{

        function __construct(){
            parent::__construct();
        }

        /**
         * 课本列表
         * @param  integer $title [description]
         * @param  integer $limit [description]
         * @return [type]         [description]
         */
        function index($title = 0,$subject=0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $subject = $post_data['subject'] ? $post_data['subject'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('title' => $title);
            $conditions = array('subject' => $subject);

            $this->load->model("Subject_book_model");
            $result = $this->Subject_book_model->get_book_list($limit, $this->offset, $conditions);

            $this->load->library('pagination');
            $page_config['base_url'] = '/subject/index/' . $title.'/'.$subject;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 5;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();

            //获取科目
            $this->load->model("Subject_model");
            $category_list = $this->Subject_model->get_subject_all();
            $this->smarty->assign("category_list", $category_list);

            $this->smarty->assign("action", '课本管理');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("subject_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->display("subject/list.html");
        }

        /**
         * 添加课本
         */
        function add(){
            $post_data = $this->input->post();
            if($post_data){
                if(isset($post_data['file'])) unset($post_data['file']);
                if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
                $post_data['add_time'] = time();
                $this->db->insert("b_book", $post_data);
                $insert_id = $this->db->insert_id();
                if($insert_id > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                //获取科目
                $this->load->model("Subject_model");
                $category_list = $this->Subject_model->get_subject_all();
                $this->smarty->assign("category_list", $category_list);

                $this->smarty->assign("action", '添加课本');
                $this->smarty->assign("post_url", '/subject_book/add');
                $this->smarty->display("subject/save.html");
            }
        }

        /**
         * 修改课本
         * @param  integer $id [description]
         * @return [type]      [description]
         */
        function edit($id = 0){
            $post_data = $this->input->post();
            if($post_data){
                if(isset($post_data['file'])) unset($post_data['file']);
                if(isset($post_data['img'])) $post_data['img'] = get_qiniu_url($post_data['img']);
                $this->db->where('id', $post_data['id']);
                $this->db->update('b_book', $post_data);
                if($this->db->affected_rows() > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                if(intval($id) > 0){
                    $subject_info = get_field_by_where("b_book", "*", "id='{$id}'");
                    if(!$subject_info){
                        show_message($this->jump_url, "数据不存在");exit;
                    }
                    //获取科目
                    $this->load->model("Subject_model");
                    $category_list = $this->Subject_model->get_subject_all();
                    $this->smarty->assign("category_list", $category_list);

                    $this->smarty->assign("subject_info", $subject_info);
                    $this->smarty->assign("post_url", '/subject_book/edit');
                    $this->smarty->assign("action", '编辑课本');
                    $this->smarty->display("subject/save.html");
                }else{
                    show_message($this->jump_url, "非法操作");
                }
            }
        }

        /**
         * 删除课本
         * @param  integer $ids [description]
         * @return [type]       [description]
         */
        function delete($ids = 0){
            if($_POST){
                $ids = $this->input->post('ids');
            }else{
                $ids = (array) $ids;
            }
            delete_data($ids, "b_book");
            if($this->db->affected_rows() > 0){
                show_message($this->jump_url, "删除成功");
            }else{
                show_message($this->jump_url, "删除失败，请重试！");
            }
        }

        /**
         * 课本章节管理
         * @param  integer $subject_id [description]
         * @param  integer $parent_id  [description]
         * @param  integer $title      [description]
         * @param  integer $limit      [description]
         * @return [type]              [description]
         */
        function index_chapter($subject_id = 0, $parent_id = 0, $title = 0, $limit = 0){
            if($this->input->post()){
                $limit = 0;
                $post_data = $this->input->post();
                $title = $post_data['title'] ? urldecode($post_data['title']) : 0;
                $subject_id = !empty($post_data['subject_id']) ? $post_data['subject_id'] : 0;
                $parent_id = !empty($post_data['parent_id']) ? $post_data['parent_id'] : 0;
            }
            $current_url = 'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
            $conditions = array('subject_id' => $subject_id, 'parent_id' => $parent_id, 'title' => $title);
            
            $this->smarty->assign("book_id", $subject_id);

            //获取章节
            $this->load->model("Subject_book_model");
            $result = $this->Subject_book_model->get_chapter_list($limit, $this->offset, $conditions);

            //分页
            $this->load->library('pagination');
            $page_config['base_url'] = '/subject_book/index_chapter/' . $subject_id . '/' . $parent_id . '/' . $title;
            $page_config['total_rows'] = $result['count'];
            $page_config['per_page'] = $this->offset;
            $page_config['uri_segment'] = 6;
            $this->pagination->initialize($page_config);
            $pagination = $this->pagination->create_links();

            //获取课本
            $subject_list = $this->Subject_book_model->get_book_list();
            $this->smarty->assign("subject_list", $subject_list['result']);

            $this->smarty->assign("action", '章节列表');
            $this->smarty->assign("conditions", $conditions);
            $this->smarty->assign("current_url", $current_url);
            $this->smarty->assign("chapter_list", $result['result']);
            $this->smarty->assign("pagination", $pagination);
            $this->smarty->display("subject/chapter_list.html");
        }

        /**
         * 添加章节
         */
        function add_chapter($book_id){
            $this->smarty->assign("book_id", $book_id);

            $post_data = $this->input->post();
            if($post_data){
                $post_data['create_on'] = time();
                $post_data['status'] = 1;
                if(!isset($post_data['is_chapter'])) $post_data['is_chapter'] = 0;
                $this->db->insert("b_book_chapter", $post_data);
                $insert_id = $this->db->insert_id();
                if($insert_id > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                
                //获取课本list
                $this->load->model("Subject_book_model");
                $subject_list = $this->Subject_book_model->get_book_list();
                $this->smarty->assign("subject_list", $subject_list['result']);

                //获取课本顶级章节
                $chapter_list_data = $this->Subject_book_model->get_chapter_by_book_parent($book_id);
                $this->smarty->assign("chapter_list_data", $chapter_list_data);

                $this->smarty->assign("action", '添加课本章节');
                $this->smarty->assign("post_url", '/subject_book/add_chapter/'.$book_id);
                $this->smarty->display("subject/chapter_save.html");
            }
        }

        /**
         * 编辑章节
         * @param  integer $id [description]
         * @return [type]      [description]
         */
        function edit_chapter($id = 0){
            $post_data = $this->input->post();
            if($post_data){
                if(!isset($post_data['is_chapter'])) $post_data['is_chapter'] = 0;
                $this->db->where('id', $post_data['id']);
                $this->db->update('b_book_chapter', $post_data);
                if($this->db->affected_rows() > 0){
                    show_message($_SERVER['HTTP_REFERER']);
                }else{
                    show_message($_SERVER['HTTP_REFERER'], "操作失败");
                }
            }else{
                if(intval($id) > 0){
                    $chapter_info = get_field_by_where("b_book_chapter", "*", "id='{$id}'");
                    if(!$chapter_info){
                        show_message($this->jump_url, "数据不存在");exit;
                    }
                    //获取课本list
                    $this->load->model("Subject_book_model");
                    $subject_list = $this->Subject_book_model->get_book_list();
                    $this->smarty->assign("subject_list", $subject_list['result']);

                    // $this->load->model("Subjects");
                    $chapter_list_data = $this->Subject_book_model->get_chapter_by_book_parent($chapter_info['book_id']);
                    
                    // $subject_list = $this->Subjects->get_subject_list();
                    // $this->smarty->assign("subject_list", $subject_list['result']);
                    $this->smarty->assign("chapter_list_data", $chapter_list_data);
                    $this->smarty->assign("chapter_info", $chapter_info);
                    
                    $this->smarty->assign("post_url", '/subject_book/edit_chapter');
                    $this->smarty->assign("action", '编辑科目');
                    $this->smarty->display("subject/chapter_save.html");
                }else{
                    show_message($this->jump_url, "非法操作");
                }
            }
        }

        function get_chapter_list($subject_id){
            $this->load->model("Topics");
            $conditions['subject_id'] = $subject_id;
            $chapter_list = $this->Topics->get_chapter($conditions);
            $chapter_list_data = array();
            $code = 0;
            if($chapter_list){
                foreach ($chapter_list as $key => $value) {
                    if($value['parent_id'] == 0){
                        $chapter_list_data[$value['id']] = $value;
                        unset($chapter_list[$key]);
                    }
                }

                foreach ($chapter_list as $key => $value) {
                    if(isset($chapter_list_data[$value['parent_id']])){
                        $chapter_list_data[$value['parent_id']]['sub'][$value['id']] = $value;
                        unset($chapter_list[$key]);
                    }
                }

                if($chapter_list AND $chapter_list_data){
                    foreach ($chapter_list_data as $key => $value) {
                        if($value['sub']){
                            foreach ($value['sub'] as $k => $v) {
                                foreach ($chapter_list as $k1 => $v1) {
                                    if($k == $v1['parent_id']){
                                        $chapter_list_data[$key]['sub'][$k]['sub'][$v1['id']] = $v1;
                                    }
                                }
                            }
                        }
                    }
                }
            }
            return $chapter_list_data;
        }

        /**
         * 删除章节
         * @param  integer $ids [description]
         * @return [type]       [description]
         */
        function delete_chapter($ids = 0){
            if($_POST){
                $ids = $this->input->post('ids');
            }else{
                $ids = (array) $ids;
            }
            delete_data($ids, "b_book_chapter");
            if($this->db->affected_rows() > 0){
                show_message($_SERVER['HTTP_REFERER'], "删除成功");
            }else{
                show_message($_SERVER['HTTP_REFERER'], "删除失败，请重试！");
            }
        }
    }